<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of orderdetailmodel
 *
 * @author lucky
 */
include_once APPPATH . "core/base_model.php";

class detailmodel extends base_model {

    private static $table = 'order_detail';

    public function __construct() {
        parent::__construct();
        $this->load->database('apach');
    }

    /**
     * 新增 订单
     *
     * @param $data
     *
     * @return null
     */
    public function newHjItemInfo($order_id, $data) {
        if (!$data || !is_array($data)) {
            return null;
        }
        foreach($data as $d){
            $iteminfo['order_id'] = $order_id;
            $iteminfo['sku_id'] = isset($d['sku_id']) ? $d['sku_id'] : '';
            $iteminfo['sku_name'] = isset($d['sku_name']) ? $d['sku_name'] : '';
            $iteminfo['price'] = isset($d['price']) ? ($d['price']/100) : 0;
            $iteminfo['num'] = isset($d['quantity ']) ? $d['quantity '] : 1;

            $iteminfo['total'] = $iteminfo['price'] * $iteminfo['num'];
            $iteminfo['sku_image'] = isset($d['sku_image']) ? $d['sku_image'] : '';

            $this->replace(self::$table, $iteminfo);
        }

    }

    /**
     * 新增 订单
     *
     * @param $data
     *
     * @return null
     */
    public function newjdItemInfo($order_id, $data) {
        if (!$data || !is_array($data)) {
            return null;
        }

        $iteminfo['order_id'] = $order_id;
        $iteminfo['sku_id'] = isset($data['sku_id']) ? $data['sku_id'] : '';
        $iteminfo['sku_name'] = isset($data['sku_name']) ? $data['sku_name'] : '';
        $iteminfo['price'] = isset($data['jd_price']) ? $data['jd_price'] : 0;
        $iteminfo['third_id'] = isset($data['ware_id']) ? $data['ware_id'] : '';
        $iteminfo['num'] = isset($data['item_total ']) ? $data['item_total '] : 1;

        $iteminfo['total'] = $iteminfo['price'] * $iteminfo['num'];
        $iteminfo['sku_image'] = isset($data['sku_image']) ? $data['sku_image'] : '';

        $result = $this->replace(self::$table, $iteminfo);
        return $result;
    }

    public function newtbItemInfo($order_id, $data) {
        if (!$data || !is_array($data)) {
            return null;
        }

        $iteminfo['order_id'] = $order_id;
        $iteminfo['sku_id'] = isset($data['sku_id']) ? $data['sku_id'] : '';
        $iteminfo['sku_name'] = isset($data['title']) ? $data['title'] : '';
        $iteminfo['price'] = isset($data['price']) ? ($data['price']/100) : 0;
        $iteminfo['third_id'] = isset($data['num_iid']) ? $data['num_iid'] : '';
        $iteminfo['num'] = isset($data['num']) ? $data['num'] : 1;

        $iteminfo['sku_name'] .= isset($iteminfo['sku_properties_name']) ? $iteminfo['sku_properties_name'] : "";
        $iteminfo['total'] = $iteminfo['price'] * $iteminfo['num'];
        $iteminfo['sku_image'] = isset($data['pic_path']) ? $data['pic_path'] : '';

        $result = $this->replace(self::$table, $iteminfo);
        return $result;
    }

    /**
     * 批量写入订单
     *
     * @param $data
     *
     * @return null
     */
    public function MoreTbItemInfo($order_id, $data) {
        if (!$order_id || !is_array($data)) {
            return null;
        }
        $result = true;
        foreach ($data as $order) {
            $result = $this->newtbItemInfo($order_id, $order);
        }
        return $result;
    }

}
